// components/camera.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    // imgSrc:'',//最后一张小预览图
    // list:[],//拍照生成的图片list
    // num:0,//list长度
    isOpen: false,//是否开启闪光灯
    openSettingBtnShow: false //是否显示授权按钮
  },
  /**
   * 组件的方法列表
   */
  methods: {
    setFlash() {
      this.setData({
        isOpen: !this.data.isOpen
      })
    },
    handleSetting: function (e) {
      let that = this;
      // 对用户的设置进行判断，如果没有授权，即使用户返回到保存页面，显示的也是“去授权”按钮；同意授权之后才显示保存按钮
      if (!e.detail.authSetting['scope.camera']) {
        wx.showModal({
          title: '警告',
          content: '若不打开授权，则无法拍照！',
          showCancel: false
        })
        that.setData({
          openSettingBtnShow: true
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '您已授权，可以拍照了！',
          showCancel: false,
          success: function () {
            wx.navigateBack({
              delta: 1,
            })
          }
        })
        that.setData({
          openSettingBtnShow: false
        })
      }
    },
    takePhoto() {
      let that = this;
      let isIoscamera = false;
      try {
        let platform = wx.getSystemInfoSync();
        if (platform.platform == 'ios') {
          isIoscamera = true
        };
      } catch (e) {
        console.log('Error:', e);
      }


      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          console.log(res)
          that.triggerEvent("setShow", {
            show: true
          });
          app.upOrcInvoiceImage(res, null, null, isIoscamera);
          // that.saveImg(res.tempImagePath)
        },
        fail: (err) => {
          that.triggerEvent("setShow", {
            show: false
          });
        }
      })
    },
    saveImg(path) {
      console.log(this.data.imgSrc)
      wx.saveImageToPhotosAlbum({
        filePath: path,
        success(res) { }
      })
    },
    //选择本地图片
    openAlbum() {
      var that = this;
      let sizeType = ['original'];
      try {
        let platform = wx.getSystemInfoSync();
        if (platform.platform == 'ios') {
          sizeType = ['original', 'compressed'];
        };
      } catch (e) {
        console.log('Error:', e);
      }
      wx.chooseImage({
        count: 1,
        sizeType: sizeType,
        sourceType: ['album'],
        success(res) {
          that.triggerEvent("setShow", {
            show: true
          });
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          setTimeout(function () {
            app.upOrcInvoiceImage(res);
          }, 100)

        },
        fail() {
          that.triggerEvent("setShow", {
            show: false
          });
        }
      })
    },
    cameraError(e) {//用户不允许使用摄像头时触发
      wx.showModal({
        title: '警告',
        content: '若不打开授权，则无法拍照！',
        showCancel: false
      })
      this.setData({
        openSettingBtnShow: true
      })
    },
    cameraStop(e) {//摄像头在非正常终止时触发
      console.log('相机错误')
      console.log(e)
      // wx: wx.navigateBack({
      //   delta: 1,
      // })
    }
  }
})
